package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月27日
*时间：下午1:34:54
*/
public class Q6 {
    /**
     * @param A and B: sorted integer array A and B.
     * @return: A new sorted integer array
     */
    public int[] mergeSortedArray(int[] A, int[] B) {
        if (A == null || A.length == 0) {
            return B;
        }
        if (B == null || B.length == 0) {
            return A;
        }
        int lA = A.length;
        int lB = B.length;
        int pointA = 0;
        int pointB = 0;
        int rs[] = new int[lA + lB];
        while (pointA < lA && pointB < lB) {
            if (A[pointA] > B[pointB]) {
                rs[pointA + pointB] = B[pointB];
                pointB++;
            } else if (A[pointA] < B[pointB]) {
                rs[pointA + pointB] = A[pointA];
                pointA++;
            } else {
                rs[pointA + pointB] = B[pointB];
                pointB++;
                rs[pointA + pointB] = A[pointA];
                pointA++;
            }
        }
        while (pointA < lA) {
            rs[pointA + pointB] = A[pointA];
            pointA++;
        }
        while (pointB < lB) {
            rs[pointA + pointB] = B[pointB];
            pointB++;
        }
        return rs;
    }

    @Test
    public void TestA() {
        int A[] = { 1, 2, 3, 4 };
        int B[] = { 2, 4, 5, 6 };
        System.out.println(mergeSortedArray(A, B));
    }
}
